<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('工程师工作计划详情')"/>
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <th:block th:include="include :: select2-css" />
    <style>
        .select2-container--default .select2-selection--single{
            border: 1px solid #ddd;
        }
    </style>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <input type="hidden" th:value="${tlId}" id="tlId">
                <input type="hidden" th:value="${fseId}" id="fseId">
                <input type="hidden" th:value="${roleId}" id="roleId">


                <div class="select-list">
                    <ul>
                        <!-- <li>
                             <p STYLE="width: auto">工程师姓名：</p>
                             <input type="text" name="fseName"/>
                         </li>-->
                        <li class="select-time">
                            <label>时间： </label>
                            <input type="text" class="time-input" id="startTime" placeholder="开始时间" name="startTime"/>
                            <span>-</span>
                            <input type="text" class="time-input" id="endTime" placeholder="结束时间" name="endTime"/>
                        </li>

                        <li class="select-time level_param" id="param_bu">
                            <label>BU： </label>
                            <input type="text" class="form-control" id="buName" name="bu" maxlength="10"
                                   placeholder="请输入BU" onchange="buChange();"/>
                            <input type="text" id="bu" hidden/>
                            <input type="text" id="buStr" hidden/>
                        </li>
                        <li class="level_param" id="param_tl">
                            <label>TL： </label>
                            <!--<input type="text" class="form-control" id="teamleaderName" maxlength="10"-->
                            <!--placeholder="请输入TL" onchange="teamleaderChange();"/>-->
                            <!--<input type="text" id="teamleaderId" name="teamleaderId" hidden/>-->
                            <!--<input type="text" id="teamleaderStr" hidden/>-->
                            <select class="js-example-basic-single" id="teamleaderId"  name="teamleaderId">
                                <option value="">请选择</option>
                            </select>
                        </li>
                        <li class="level_param" id="param_fse">
                            <label>FSE： </label>
                            <!--<input type="text" class="form-control" id="enginnerName" maxlength="10"-->
                            <!--placeholder="请输入FSE"/>-->
                            <!--<input type="text" id="enginnerId" name="enginnerId" hidden/>-->
                            <!--<input type="text" id="enginnerStr" hidden/>-->
                            <select class="js-example-basic-single" id="enginnerId"  name="enginnerId">
                                <option value="">请选择</option>
                            </select>
                        </li>

                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="resetForm()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                            <!-- 导出按钮-->
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.table.exportExcel()"
                               shiro:hasPermission="fsepc:details:export">
                                <i class="fa fa-download"></i> 导出
                            </a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <!--<div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="fsepc:details:add">
                <i class="fa fa-plus"></i> 添加
            </a>
            <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="fsepc:details:edit">
                <i class="fa fa-edit"></i> 修改
            </a>
            <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="fsepc:details:remove">
                <i class="fa fa-remove"></i> 删除
            </a>
            <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="fsepc:details:export">
                <i class="fa fa-download"></i> 导出
             </a>
        </div>-->
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: bootstrap-fileinput-js"/>
<th:block th:include="include :: select2-js" />
<script th:src="@{/ajax/libs/typeahead/bootstrap3-typeahead.min.js}"></script>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('fsepc:details:edit')}]];
    var removeFlag = [[${@permission.hasPermi('fsepc:details:remove')}]];
    enginnerInit2();
    teamleaderInit2();
    var prefix = ctx + "fsepc/details";
    //    $("#enginnerId").on("change",function () {
    //        enginnerInit2();
    //    })
    $("#teamleaderId").on("change",function () {
        //  teamleaderInit2();
        enginnerInit2();
    })
    function resetForm() {
        $("#startTime").val("");
        $("#endTime").val("");
        $("#buName").val("");
        window.location = '/fsepc/details'
        // $('#enginnerId').select2("val","");
        // $('#teamleaderId').select2("val","");
    }



    function buChange() {
        var buName = $("#buName").val();
        var buStr = $("#buStr").val();
        if (buStr.indexOf(buName) < 0) {
            $("#bu").val("暂无数据！！");
        }
        //  teamleaderInit();
        // enginnerInit();
    }

    //    function teamleaderChange() {
    //        var teamleaderName = $("#teamleaderName").val();
    //        var teamleaderStr = $("#teamleaderStr").val();
    //        if (teamleaderStr.indexOf(teamleaderName) < 0) {
    //            $("#teamleaderId").val("暂无数据！！");
    //        }
    //        enginnerInit();
    //    }

    $(function () {
        var roleId = $("#roleId").val();
        if (roleId == 3) {
            $("#param_fse").hide();
            $("#param_tl").hide();
            $("#param_bu").hide();

            $("#enginnerId").val($("#fseId").val());

        } else if (roleId == 4) {
            $("#param_fse").show();
            $("#param_tl").hide();
            $("#param_bu").hide();
            $("#teamleaderId").val($("#fseId").val());
            //  $("#teamleaderId").val($("#tlId").val());
            enginnerInit2();
        } else {
            $("#param_fse").show();
            $("#param_tl").show();
            $("#param_bu").show();

            buInit();

        }
        var options = {
            url: prefix + "/selectFseWorkPlanPC",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "工程师工作计划详情",
            columns: [
                {
                    field: 'fseName',
                    title: 'FSE'
                },
                {
                    field: 'estimatedVisitingTime',
                    title: '预计上门时间',
                    formatter: function (value, row, index) {
                        if (row.estimatedVisitingTime && row.estimatedDepartureTime) {
                            /*  var start = new Date(row.estimatedVisitingTime);
                              var date = start.Format("yyyy-MM-dd HH:mm:ss");
                              var week = getWeekDate(start);*/
                            value = value.substring(0, 16);
                            return value;
                        }
                    }

                },
                {
                    field: 'estimatedDepartureTime',
                    title: '预计离开时间',
                    formatter: function (value, row, index) {
                        if (row.estimatedVisitingTime && row.estimatedDepartureTime) {
                            /*     var end = new Date(row.estimatedDepartureTime);
                                 var date = end.Format("yyyy-MM-dd HH:mm:ss");
                                 var week = getWeekDate(end);*/
                            value = value.substring(0, 16);
                            return value;
                        }
                    }
                },

                /*  {
                      field : 'estimatedDepartureTime',
                      title : '时间',
                      formatter: function(value, row, index) {
                          if (row.estimatedVisitingTime && row.estimatedDepartureTime) {
                              var start = new Date(row.estimatedVisitingTime);
                              var end = new Date(row.estimatedDepartureTime);
                              var date = start.Format("yyyy-MM-dd");
                              var startTime = start.Format("HH:mm");
                              var endTime = end.Format("HH:mm");
                              var week = getWeekDate(start);
                              return date + " " + startTime + "-" + endTime + " " + week;
                          }
                          // var actions = [];
                          // actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                          // actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                          // return actions.join('');
                      }
                  },*/
                {
                    field: 'customerName',
                    title: '客户姓名'
                },
                {
                    field: 'countNum',
                    title: '服务通知数量'
                }//,
                // {
                //     title: '操作',
                //     align: 'center',
                //     formatter: function(value, row, index) {
                //         var actions = [];
                //         actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                //         actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                //         return actions.join('');
                //     }
                // }
            ]
        };
        $.table.init(options);
    });

    Date.prototype.Format = function (fmt) {
        var o = {
            "M+": this.getMonth() + 1, //月份
            "d+": this.getDate(), //日
            "H+": this.getHours(), //小时
            "m+": this.getMinutes(), //分
            "s+": this.getSeconds(), //秒
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度
            "S": this.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" +
                o[k]).substr(("" + o[k]).length)));
        return fmt;
    }

    // 获取日期为星期几，day为日期类型
    function getWeekDate(day) {
        var weeks = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
        var week = weeks[day.getDay()];
        return week;
    }

    function buInit() {
        $.ajax({
            "url": ctx + "system/user/selectBuList",
            "type": "post",
            "async": false,
            "dataType": "json",
            "beforeSend": function () {
                $.modal.loading("正在处理中，请稍后...");
            },
            "success": function (result) {
                $.modal.closeLoading();
                var data = [];
                var buStr = "";
                if (result.data != null && result.data != "undefined")
                    for (var i = 0; i < result.data.length; i++) {
                        data.push({
                            "key": result.data[i].bu,
                            "value": result.data[i].bu
                        });
                        buStr += result.data[i].name + ", ";
                    }
                // console.log(buStr)
                $("#buStr").val(buStr);
                $("#buName").typeahead({
                    source: data,
                    displayText: function (item) {
                        return item.value;//返回字符串
                    },
                    afterSelect: function (item) {
                        $("#buId").val(item.key);
                    }
                });
            }
        });
        $.modal.closeLoading();
    }

    function teamleaderInit() {
        $.ajax({
            "url": ctx + "system/user/selectTeamleaderList",
            "data": {
                "bu": $("#buName").val()
            },
            "type": "post",
            "async": false,
            "dataType": "json",
            "beforeSend": function () {
                $.modal.loading("正在处理中，请稍后...");
            },
            "success": function (result) {
                $.modal.closeLoading();
                var data = [];
                var teamleaderStr = "";
                if (result.data != null && result.data != "undefined")
                    for (var i = 0; i < result.data.length; i++) {
                        data.push({
                            "key": result.data[i].teamleaderId,
                            "value": result.data[i].userName
                        });
                        teamleaderStr += result.data[i].userName + ", ";
                    }
                // console.log(teamleaderStr)
                $("#teamleaderStr").val(teamleaderStr);
                $("#teamleaderName").typeahead({
                    source: data,
                    displayText: function (item) {
                        return item.value;//返回字符串
                    },
                    afterSelect: function (item) {
                        $("#teamleaderId").val(item.key);
                    }
                });
            }
        });
        $.modal.closeLoading();
    }

    //    function enginnerInit() {
    //        $.ajax({
    //            "url": ctx + "system/user/selectEnginnerList",
    //            "data": {
    //                "bu": $("#buName").val(),
    //                "teamleaderId": $("#teamleaderId").val()
    //            },
    //            "type": "post",
    //            "async": false,
    //            "dataType": "json",
    //            "beforeSend": function () {
    //                $.modal.loading("正在处理中，请稍后...");
    //            },
    //            "success": function (result) {
    //                $.modal.closeLoading();
    //                var data = [];
    //                var enginnerStr = "";
    //                if (result.data != null && result.data != "undefined")
    //                    for (var i = 0; i < result.data.length; i++) {
    //                        data.push({
    //                            "key": result.data[i].enginnerId,
    //                            "value": result.data[i].userName
    //                        });
    //                        enginnerStr += result.data[i].userName + ", ";
    //                    }
    //                // console.log(enginnerStr)
    //                $("#enginnerStr").val(enginnerStr);
    //                $("#enginnerName").typeahead({
    //                    source: data,
    //                    displayText: function (item) {
    //                        return item.value;//返回字符串
    //                    },
    //                    afterSelect: function (item) {
    //                        // alert(item.key);
    //                        $("#enginnerId").val(item.key);
    //                    }
    //                });
    //            }
    //        });
    //        $.modal.closeLoading();
    //    }

    function teamleaderInit2() {
        $.ajax({
            "url": ctx + "system/user/selectTeamleaderList",
            "data": {
                "bu": $("#buName").val()
            },
            "type": "post",
            "async": false,
            "dataType": "json",
            "success": function (result) {
                $.modal.closeLoading();
                var data1 = [];
                if (result.data != null && result.data != "undefined")
                    for (var i = 0; i < result.data.length; i++) {
                        data1.push({
                            "id": result.data[i].teamleaderId,
                            "text": result.data[i].userName
                        });
                    }
                $("#teamleaderId").select2({
                    data: data1,
                    width: '120px'
                })
                //$('#teamleaderId').select2("val","");
            }
        });

    }

    function enginnerInit2() {
        var data1 = [];
        $("#enginnerId").select2({
            data: data1,
            width: '120px'
        })
        $.ajax({
            "url": ctx + "system/user/selectEnginnerList",
            "data": {
                "bu": $("#buName").val(),
                "teamleaderId": $("#teamleaderId").val()
            },
            "type": "post",
            "async": false,
            "dataType": "json",
            "success": function (result) {
                if (result.data != null && result.data != "undefined")
                    var str = "<option value=''>请选择</option>"
                for (var i = 0; i < result.data.length; i++) {
                    str +=  "<option value='"+  result.data[i].enginnerId+"'> "+result.data[i].userName +"</option>"
                }
                $("#enginnerId").html(str)

            }
        });
    }


</script>
</body>
</html>